﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonHelper;

namespace WebsitSetup
{
    public partial class CreateData : Form
    {
        /// <summary>
        /// 上一步的对象
        /// </summary>
        public SetServer setServer
        { get; set; }


        public CreateData()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="setServer">上一步的对象</param>
        public CreateData(SetServer setServer)
        {
            this.setServer = setServer;
            InitializeComponent();

            this.cbType.Text = "神通数据库";
            this.cbType.Enabled = false;
            this.txtAddress.Text = CommonMethod.GetAddress();

            this.txtDataName.Text = "BarryTest";
            this.txtName.Text = "sysdba";
            this.txtPass.Text = "szoscar55";
            this.txtPort.Text = "2004";

            this.txtName.Focus();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要退出吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            this.setServer.Show();
            this.Hide();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            SaveConnectionStr();
        }

        private void SaveConnectionStr()
        {
            var type = this.cbType.Text.Trim();
            var address = this.txtAddress.Text.Trim();
            var dataName = this.txtDataName.Text.Trim();
            var name = this.txtName.Text.Trim();
            var pass = this.txtPass.Text.Trim();
            var port = this.txtPort.Text.Trim();

            if (CommonMethod.checkString(dataName, name, pass, port))
            {
                try
                {
                    // 设置Unity中数据库字符串的值
                    ConfigHelper.SetConnectionStr(address, dataName, name, pass, port);

                    // 读取Sql文本中的脚本
                    var allSqlList = FileHelper.GetAllSql();

                    //创建数据库
                    var oscarHelper = new OscarHelper(address, dataName, name, pass, port);
                    oscarHelper.CreateOscarDataBase(allSqlList);

                    var publishIIS = new PublishIIS(this);
                    publishIIS.Show();
                    this.Hide();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("创建数据库时出错," + ex.Message);
                }               
            }
            else
            {
                MessageBox.Show("数据库名称、用户名、密码、端口号都不能为空.");
            }
        }
    }
}